﻿bob.ns.setNs('bob.event.tests', {

    TestEvent: function () {

        this.test_Event = function () {

            var evt = new bob.event.Event();

            //get caller.
            bob.assert.isTrue(bob.utils.isFunction(evt.get_caller), '0');
            var caller = evt.get_caller();
            bob.assert.isTrue(bob.utils.isFunction(caller), '0.1');
            bob.assert.isUndefined(evt.get_caller);

            //add handler.
            bob.assert.throwsError(function () { evt.add_handler(null); }, '1');

            var eventArg = null;
            var handler = function (arg) {
                eventArg = arg;
            };

            bob.assert.equals(handler, evt.add_handler(handler), '2');
            //call #1.
            var arg = 1;
            caller(arg);
            bob.assert.equals(eventArg, arg, '3');

            //remove handler.
            evt.remove_handler(handler);
            //call #2.
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(eventArg, arg, '4');

            //several handlers.
            var handler1_arg = 0;
            var handler1 = function (arg) {
                handler1_arg = arg;
            };

            var handler2_arg = 0;
            var handler2 = function (arg) {
                handler2_arg = arg;
            };

            evt.add_handler(handler1);
            evt.add_handler(handler2);
            var arg = 1;
            caller(arg);
            bob.assert.equals(handler1_arg, arg, '5');
            bob.assert.equals(handler2_arg, arg, '6');

            evt.remove_handler(handler1);
            evt.remove_handler(handler2);
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(handler1_arg, arg, '7');
            bob.assert.notEquals(handler2_arg, arg, '8');

            //several arguments.
            evt = new bob.event.Event();
            caller = evt.get_caller();

            var v1 = 1;
            var v2 = 2;
            handler = function (a, b) {
                bob.assert.equals(v1, a, '9');
                bob.assert.equals(v2, b, '10');
            };
            evt.add_handler(handler);
            caller(v1, v2);

            v1 = 3;
            v2 = 4;
            caller(v1, v2);
        };

        this.test_namedEventWithName = function () {

            var obj = {};

            //declare event.
            var caller = bob.event.namedEvent(obj, 'onCall');
            bob.assert.isTrue(bob.utils.isFunction(caller), '0');

            //add handler.
            bob.assert.throwsError(function () { obj.add_onCall(null); }, '1');

            var eventArg = null;
            var handler = function (arg) {
                eventArg = arg;
            };

            bob.assert.equals(handler, obj.add_onCall(handler), '2');
            //call #1.
            var arg = 1;
            caller(arg);
            bob.assert.equals(eventArg, arg, '3');

            //remove handler.
            obj.remove_onCall(handler);
            //call #2.
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(eventArg, arg, '4');

            //several handlers.
            var handler1_arg = 0;
            var handler1 = function (arg) {
                handler1_arg = arg;
            };

            var handler2_arg = 0;
            var handler2 = function (arg) {
                handler2_arg = arg;
            };

            obj.add_onCall(handler1);
            obj.add_onCall(handler2);
            var arg = 1;
            caller(arg);
            bob.assert.equals(handler1_arg, arg, '5');
            bob.assert.equals(handler2_arg, arg, '6');

            obj.remove_onCall(handler1);
            obj.remove_onCall(handler2);
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(handler1_arg, arg, '7');
            bob.assert.notEquals(handler2_arg, arg, '8');
        };

        this.test_namedEventWithEventObj = function () {

            var obj = {};

            //declare event.
            var caller = bob.event.namedEvent(true, obj, 'onCall', new bob.event.Event());
            bob.assert.isTrue(bob.utils.isFunction(caller), '0');

            //add handler.
            bob.assert.throwsError(function () { obj.add_onCall(null); }, '1');

            var eventArg = null;
            var handler = function (arg) {
                eventArg = arg;
            };

            bob.assert.equals(handler, obj.add_onCall(handler), '2');
            //call #1.
            var arg = 1;
            caller(arg);
            bob.assert.equals(eventArg, arg, '3');

            //remove handler.
            obj.remove_onCall(handler);
            //call #2.
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(eventArg, arg, '4');

            //several handlers.
            var handler1_arg = 0;
            var handler1 = function (arg) {
                handler1_arg = arg;
            };

            var handler2_arg = 0;
            var handler2 = function (arg) {
                handler2_arg = arg;
            };

            obj.add_onCall(handler1);
            obj.add_onCall(handler2);
            var arg = 1;
            caller(arg);
            bob.assert.equals(handler1_arg, arg, '5');
            bob.assert.equals(handler2_arg, arg, '6');

            obj.remove_onCall(handler1);
            obj.remove_onCall(handler2);
            var arg = 2;
            caller(arg);
            bob.assert.notEquals(handler1_arg, arg, '7');
            bob.assert.notEquals(handler2_arg, arg, '8');
        };
    }

});